package com.stylefeng.guns.rest.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.huangliusong.api.user.UserAPI;
import com.huangliusong.api.user.VO.UserInfoModel;
import com.huangliusong.api.user.VO.UserModel;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.rest.common.persistence.dao.MoocUserTMapper;
import com.stylefeng.guns.rest.common.persistence.model.MoocUserT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * @author huangliusong
 * @since 2019/1/8
 * {@link }
 */
@Component
@Service(interfaceClass = UserAPI.class)
public class UserServiceImpl implements UserAPI {

    @Autowired
    private MoocUserTMapper moocUserTMapper;

    @Override
    public int login(String username, String password) {
        MoocUserT moocUserT=new MoocUserT();
        moocUserT.setUserName(username);
        MoocUserT result=moocUserTMapper.selectOne(moocUserT);
        if (result!=null&&result.getUuid()>0){
            String md5Password=MD5Util.encrypt(password);
            if (result.getUserPwd().equals(md5Password)){
                return result.getUuid();
            }
        }
        return 0;
    }

    @Override
    public boolean register(UserModel userModel) {
        // 获取注册信息
        //将注册信息实体转换为数据实体
        MoocUserT moocUserT=new MoocUserT();
        moocUserT.setUserName(userModel.getUsername());
        moocUserT.setEmail(userModel.getEmail());
        moocUserT.setAddress(userModel.getAddress());
        moocUserT.setUserPhone(userModel.getPhone());
        String m =MD5Util.encrypt(userModel.getPassword());
        moocUserT.setUserPwd(m);
        Integer integer=moocUserTMapper.insert(moocUserT);
        if (integer>0){
            return true;
        }else
        {
            return false;
        }

    }



    @Override
    public boolean checkUsername(String username) {
        EntityWrapper<MoocUserT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("user_name",username);
        Integer result=moocUserTMapper.selectCount(entityWrapper);
        if (result!=null&&result>0){
            return false;
        }else
        {
            return  true;
        }
    }

    @Override
    public boolean logout() {
        return false;
    }

    private UserInfoModel do2UserInfo(MoocUserT moocUserT){
        UserInfoModel userInfoModel=new UserInfoModel();
        userInfoModel.setHeadAddress(moocUserT.getHeadUrl());
        userInfoModel.setPhone(moocUserT.getUserPhone());
        userInfoModel.setUpdateTime(moocUserT.getUpdateTime().getTime());
        userInfoModel.setEmail(moocUserT.getEmail());
        userInfoModel.setUsername(moocUserT.getUserName());
        userInfoModel.setNickname(moocUserT.getNickName());
        userInfoModel.setBirthday(moocUserT.getBirthday());
        userInfoModel.setAddress(moocUserT.getAddress());
        userInfoModel.setSex(moocUserT.getUserSex());
        userInfoModel.setBeginTime(moocUserT.getBeginTime().getTime());
        userInfoModel.setLifeState(moocUserT.getLifeState());
        userInfoModel.setBiography(moocUserT.getBiography());
        userInfoModel.setUuid(moocUserT.getUuid());
        return userInfoModel;

    }
    @Override
    public UserInfoModel getUserInfo(int uuid) {
        //根据主键查询用户对象
        MoocUserT moocUserT=moocUserTMapper.selectById(uuid);
        UserInfoModel userInfoModel=do2UserInfo(moocUserT);
        return userInfoModel;
    }

    @Override
    public UserInfoModel updateUserInfo(UserInfoModel userInfoModel) {
        MoocUserT moocUserT=new MoocUserT();
        moocUserT.setUuid(userInfoModel.getUuid());
        moocUserT.setNickName(userInfoModel.getNickname());
        moocUserT.setLifeState(userInfoModel.getLifeState());
        moocUserT.setBirthday(userInfoModel.getBirthday());
        moocUserT.setBeginTime(time2Date(userInfoModel.getBeginTime()));
        moocUserT.setHeadUrl(userInfoModel.getHeaderUrl());
        moocUserT.setAddress(userInfoModel.getAddress());
        moocUserT.setEmail(userInfoModel.getEmail());
        moocUserT.setUserPhone(userInfoModel.getPhone());
        moocUserT.setUserSex(userInfoModel.getSex());
        moocUserT.setUpdateTime(time2Date(System.currentTimeMillis()));
        Integer isSuccess=moocUserTMapper.updateById(moocUserT);
        if (isSuccess>0){
            UserInfoModel userInfo=getUserInfo(moocUserT.getUuid());
            return userInfo;
        }else {
            return userInfoModel;
        }
    }
    private Date  time2Date(long time){
        Date date=new Date(time);
        return date;
    }
}
